package com.itfreer.bpm.workflow.utils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.itfreer.bpm.config.entity.BpmConfigEntity;
import com.itfreer.bpm.config.entity.BpmTaskConfigEntity;
import com.itfreer.bpm.config.service.IBpmConfigService;
import com.itfreer.bpm.config.service.IBpmTaskConfigService;
import com.itfreer.bpm.flow.api.IBpmProjectEntity;
import com.itfreer.bpm.history.dao.IBpmHistoryDao;
import com.itfreer.bpm.history.entity.BpmHistoryEntity;
import com.itfreer.bpm.option.para.TaskInfo;
import com.itfreer.bpm.option.para.TaskType;
import com.itfreer.bpm.power.para.UserInfo;
import com.itfreer.bpm.randomUtls.RandomUtils;
import com.itfreer.bpm.timeliness.service.TimeLinessUtil;

/**
 * 历史构建工具
 * @author gj
 *
 */
@Component
public class BpmHistoryEditUtil {
	
	/**
	 * 办理过程的数据访问层
	 */
	@Autowired
	protected IBpmHistoryDao historyDao;
	
	@Autowired
	private IBpmConfigService bpmConfigService;
	
	@Autowired
	private IBpmTaskConfigService bpmTaskConfigService;
	
	
	
	/**
	 * 时效计算模块
	 */
	@Autowired
	TimeLinessUtil timeLiness;
	
	/**
	 * 删除草稿记录
	 * @param bussinessId
	 */
	public void deleteDraft(String bussinessId) {
		if(bussinessId==null || "".equals(bussinessId.trim())) {
			return;
		}
		
		HashMap<String,Object> where=new HashMap<String,Object>();
		where.put("sbussinessid:=", bussinessId);
		where.put("optresult:=","草稿");
		
		List<BpmHistoryEntity> lst=this.historyDao.getEntitys(null, where, null, 1, 1);
		if(lst==null || lst.size()<1) return;
		
		for(BpmHistoryEntity ih:lst) {
			this.historyDao.delete(ih.getsid());
		}
		
	}
	
	/**
	 * 流程启动时的日志处理
	 * @param sexeid
	 * @param definedid
	 * @param user
	 * @param entity
	 */
	public void addStartHistory(String sexeid,String definedid,String nextTaskName,UserInfo user,IBpmProjectEntity entity) {
		BpmHistoryEntity historyEntity=new BpmHistoryEntity();
		historyEntity.setsexeid(sexeid);
		historyEntity.setactdefid(definedid);
		historyEntity.setsid(RandomUtils.createRandomGuid());
		historyEntity.sethistorystatus("0");
		//3-a用户信息处理
		/*historyEntity.setfromassigneesid(user.getUserId());
		historyEntity.setfromassigneesname(user.getUserName());
		historyEntity.setfromorgid(user.getUserOrgCode());
		historyEntity.setfromorgname(user.getUserOrgName());*/
		
		//3-b来源节点信息
		/*historyEntity.setfromtaskdefid("U0");
		historyEntity.setfromtaskdefname("开始");
		historyEntity.setfromtime(new Date());*/
		//3-c获取流程基本信息
		Map<String,Object> bpmConfigWhereMap=new HashMap<String,Object> ();
		bpmConfigWhereMap.put("actdefid:=", definedid);
		List<BpmConfigEntity> bpmConfigEntitys= bpmConfigService.getEntitys(null, bpmConfigWhereMap, null, 1, 1);
		BpmConfigEntity bpmConfigEntity =bpmConfigEntitys.get(0);
		historyEntity.setactdefname(bpmConfigEntity.getActdefname());
		historyEntity.setprjforname(bpmConfigEntity.getPrjforname());
		historyEntity.setprjvi(bpmConfigEntity.getPrjvi());
		historyEntity.setprjurl(bpmConfigEntity.getPrjurl());
		historyEntity.setactgroup(bpmConfigEntity.getActgroup());
		historyEntity.setactlt(bpmConfigEntity.getTltotal());
		//3-d获取项目名称 
		IBpmProjectEntity prjEntity=(IBpmProjectEntity)entity;
		historyEntity.setsbusiness(prjEntity.getProjectname());
		historyEntity.setSbussinessid(prjEntity.getId());
		//3-e 流程环节信息
		historyEntity.settaskdefid("U0");
		historyEntity.settaskdefname("开始");
		//3-f流程环节配置信息
		HashMap<String,Object> task_where=new HashMap<String,Object>();
		task_where.put("actdefid:=", definedid);
		task_where.put("taskdefid:=", "U0");
		
		List<BpmTaskConfigEntity> task_configs=bpmTaskConfigService.getEntitys(null, task_where, null, 1, 1);
		if(task_configs!=null && task_configs.size()>0) {
			BpmTaskConfigEntity taskConfig=task_configs.get(0);
			historyEntity.setTasktab(taskConfig.getPrjtab());
			historyEntity.settaskgroup(taskConfig.getTaskgroup());
			historyEntity.settasktl(taskConfig.getTasktl());
		}
		
		//3-g流程参与人信息
		String sassignees=","+user.getUserId()+","+user.getUserName();
		historyEntity.setsassignees(sassignees);
		
		//3-h操作信息
		historyEntity.setoptresult("创建");
		historyEntity.setoptassigneesid(user.getUserId());
		historyEntity.setoptassigneesname(user.getUserName());
		historyEntity.setoptcontent("");
		historyEntity.setoptorgid(user.getUserOrgCode());
		historyEntity.setoptorgname(user.getUserOrgName());
		historyEntity.setopttime(new Date());
		
		//3-i设置是否被阅读
		historyEntity.setSread("1");
		
		//3-j 设置项目代办人信息
		historyEntity.setToptassigneese(","+user.getUserId()+","+user.getUserName());
		
		historyEntity.setNexttask(nextTaskName);
		
		//3-k 节点类型，方便构建历史问题
		historyEntity.setTasktype(TaskType.CommonTask.toString());
		
		//4、保存流程审批历史信息
		historyDao.add(historyEntity);
		timeLiness.computeTimeLiness(historyEntity, false);
	}
	
	/**
	 * 没保存，不需要计算时效问题
	 * @param sexeid
	 * @param definedid
	 * @param user
	 * @param userTask
	 * @param entity
	 * @param fromTime
	 * @return
	 */
	public BpmHistoryEntity CreateHistory(String sexeid,String definedid,
		UserInfo user,TaskInfo userTask,
		IBpmProjectEntity entity,Date fromTime) {
	//3、办理过程处理
			BpmHistoryEntity historyEntity=new BpmHistoryEntity();
			historyEntity.setsexeid(sexeid);
			historyEntity.setactdefid(definedid);
			historyEntity.setsid(RandomUtils.createRandomGuid());
			//historyEntity.sethistorystatus(historyStatus);
			//3-a用户信息处理
			historyEntity.setfromassigneesid(user.getUserId());
			historyEntity.setfromassigneesname(user.getUserName());
			historyEntity.setfromorgid(user.getUserOrgCode());
			historyEntity.setfromorgname(user.getUserOrgName());
			
			//3-b来源节点信息
	/*		historyEntity.setfromtaskdefid(fromTaskId);
			historyEntity.setfromtaskdefname(FromTaskName);*/
			historyEntity.setfromtime(fromTime);
			//3-c获取流程基本信息
			Map<String,Object> bpmConfigWhereMap=new HashMap<String,Object> ();
			bpmConfigWhereMap.put("actdefid:=", definedid);
			List<BpmConfigEntity> bpmConfigEntitys= bpmConfigService.getEntitys(null, bpmConfigWhereMap, null, 1, 1);
			BpmConfigEntity bpmConfigEntity =bpmConfigEntitys.get(0);
			historyEntity.setactdefname(bpmConfigEntity.getActdefname());
			historyEntity.setprjforname(bpmConfigEntity.getPrjforname());
			historyEntity.setprjvi(bpmConfigEntity.getPrjvi());
			historyEntity.setprjurl(bpmConfigEntity.getPrjurl());
			historyEntity.setactgroup(bpmConfigEntity.getActgroup());
			historyEntity.setactlt(bpmConfigEntity.getTltotal());
			//3-d获取项目名称 
			IBpmProjectEntity prjEntity=(IBpmProjectEntity)entity;
			historyEntity.setsbusiness(prjEntity.getProjectname());
			historyEntity.setSbussinessid(prjEntity.getId());
			
			if(userTask!=null) {
				//3-e 流程环节信息
				historyEntity.settaskdefid(userTask.getTaskdefinedid());
				historyEntity.settaskdefname(userTask.getTaskName());
				//3-f流程环节配置信息
				HashMap<String,Object> task_where=new HashMap<String,Object>();
				task_where.put("actdefid:=", definedid);
				task_where.put("taskdefid:=", userTask.getTaskdefinedid());
				
				List<BpmTaskConfigEntity> task_configs=bpmTaskConfigService.getEntitys(null, task_where, null, 1, 1);
				if(task_configs!=null && task_configs.size()>0) {
					BpmTaskConfigEntity taskConfig=task_configs.get(0);
					historyEntity.setTasktab(taskConfig.getPrjtab());
					historyEntity.settaskgroup(taskConfig.getTaskgroup());
					historyEntity.settasktl(taskConfig.getTasktl());
				}
			}
			
			
			//3-g流程参与人信息
			//String sassignees=","+user.getUserId()+","+user.getUserName();
			//historyEntity.setsassignees(null);
			
			//3-h操作信息
			//historyEntity.setoptresult(null);
			//historyEntity.setoptassigneesid(null);
			//historyEntity.setoptassigneesname(null);
			//historyEntity.setoptcontent(null);
			//historyEntity.setoptorgid(null);
			//historyEntity.setoptorgname(null);
			
			//3-i设置是否被阅读
			//historyEntity.setSread(isRead);
			
			
			//3-j 设置项目代办人信息
			//historyEntity.setToptassigneese(assigneeses);
			
			//historyEntity.setNexttask(nextTaskName);
			
			//4、保存流程审批历史信息
			//historyDao.add(historyEntity);
			//没有进行保存操作，因此不需要添加时效计算
			//timeLiness.computeTimeLiness(historyEntity, false);
			return historyEntity;
	}
	
	/**
	 * 构建历史记录
	 * @param sexeid 流程主键
	 * @param definedid 流程id
	 * @param user 用户信息
	 * @param userTask  当前节点信息
	 * @param entity 数据库项目实体
	 */
	public BpmHistoryEntity CreateHistory(String sexeid,String definedid,UserInfo user,TaskInfo userTask,IBpmProjectEntity entity) {
		return CreateHistory(sexeid,definedid,user,userTask,entity,new Date());
	}
	
	/**
	 * 批量更新数据
	 * @param lst
	 */
	public void updateList(List<BpmHistoryEntity> lst) {
		if(lst!=null && lst.size()>0) {
			for(BpmHistoryEntity e:lst) {
				this.historyDao.update(e);
				timeLiness.computeTimeLiness(e, false);
			}
		}
	}
	
	/**
	 * 保留记录
	 * @param historyEntity
	 */
	public void addandSaveEntity(BpmHistoryEntity historyEntity) {
		this.historyDao.add(historyEntity);
		timeLiness.computeTimeLiness(historyEntity, false);
	}
	
	/**
	 * 保留记录
	 * @param historyEntity
	 */
	public void addList(List<BpmHistoryEntity> lst ) {
		if(lst!=null && lst.size()>0) {
			for(BpmHistoryEntity historyEntity:lst) {
				this.historyDao.add(historyEntity);
				timeLiness.computeTimeLiness(historyEntity, false);
			}
		}
	}
	
	/**
	 * 保留记录
	 * @param historyEntity
	 */
	public void updateEntity(BpmHistoryEntity historyEntity) {
		this.historyDao.update(historyEntity);
		timeLiness.computeTimeLiness(historyEntity, false);
	}

}
